# _*_coding:utf-8_*_
"""
   ATS 1.0(Automated Test Server, Version 1.0) Global Data Dict File,
   Developed by Ivy.Li 2013.03.27.
   (C)Copyright 2013 Hech. All rights reserved
"""

# Location Mapping dictionary, in order to locate the test task belong to which wafer, which site,...
LMD = {}

# Global Data Dictionary, extract from PMF and GDF
GDD = {}

SERVICE_CONTAINER = {}


def get_loc_info(location_ind):
    global LMD
    return LMD[location_ind]


def register_service(d):
    global SERVICE_CONTAINER
    # update方法用于更新字典的键值对
    SERVICE_CONTAINER.update(d)


def get_service(service_key):
    global SERVICE_CONTAINER
    return SERVICE_CONTAINER.get(service_key, None)


def register_db_model(model_key, db_model):
    global SERVICE_CONTAINER
    old_model = SERVICE_CONTAINER.get(model_key, None)
    if old_model is not None:
        # release source
        old_model.release_con()
        old_model = None
    SERVICE_CONTAINER[model_key] = db_model


def clearLMD():
    global LMD
    LMD = {}


def getLMD():
    """
    get current ppf's location info dict.
    dict format:
    key: 1,val: (1, 'WafSite1', (0, 3), 'SS1', 'dv1', 'ptm5')
    key:2, val: (1, 'WafSite1', (0, 3), 'SS1', 'dv1', 'ptm6')
    ... ...
   """
    global LMD
    return LMD


def getService():
    global SERVICE_CONTAINER
    return SERVICE_CONTAINER


def getGDD():
    global GDD
    return GDD


def clearGDD():
    global GDD
    Set_Manual_Run = False
    if 'Set_Manual_Run' in GDD and GDD['Set_Manual_Run']:
        Set_Manual_Run = True

    GDD = {'Fail_Dev_Num': 0, 'Total_Dev_Num': 0, 'Exit_Dev': False, 'ATS_Stop_Flag': False, 'Test_Abort': False,
           'Cur_Loop_Num': 0, 'Cur_Dev': '', 'User_Abort_UAP': None, 'NOT_END': False, 'Continue_Flag': False,
           'real_time_data': {}, 'MaxLoop': 0, 'Error_site': False, 'CURR_DB': None, 'STOP_AUTO': 0}

    # Init global variable

    # reset continue flag

    # real time data for update GUI

    # MaxLoop for the same batch run

    # SITE ERROR
    # Current dbfile for the same batch run
    # Judge whether the automation stop
    # use this flag to idendify whether it is a manual run or automation run
    if Set_Manual_Run:
        GDD['Manual_Run'] = True
    else:
        GDD['Manual_Run'] = False


def updateGDD(d):
    global GDD
    GDD.update(d)


def updateRealTimeGDD(d):
    """
    Update Realtime data dict
    """
    global GDD
    if 'real_time_data' not in GDD:
        GDD['real_time_data'] = {}

    GDD['real_time_data'].update(d)


def output_GDD():
    global GDD
    for k in GDD.keys():
        print(k, '------->', GDD[k])
